package logic

import (
	"context"
	"errors"
	"novel-mgr1/model"

	"novel-mgr1/userrpc/internal/svc"
	"novel-mgr1/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BooksDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBooksDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BooksDelLogic {
	return &BooksDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 书架下架
func (l *BooksDelLogic) BooksDel(in *userrpc.BooksDelRequest) (*userrpc.BooksDelResponse, error) {
	// todo: add your logic here and delete this line
	var findBooks model.Books
	tx := model.DB.Debug()
	err := findBooks.FindBooksById(tx, (*userrpc.BooksAddRequest)(in))
	if err != nil {
		tx.Rollback()
		return nil, err

	}
	if findBooks.ID == 0 {
		tx.Rollback()
		return nil, errors.New("不存在")
	}
	var books model.Books
	err = books.BooksDel(tx, in)
	if err != nil {
		return nil, errors.New("删除失败")
	}
	tx.Commit()
	return &userrpc.BooksDelResponse{}, nil
}
